<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use layui\Layui;
use think\Request;

class Mater extends Base
{
	//设备材质
	public function index()
	{
		$search_name = input('search_name');
        $search = ['query'=>[]];
        $search['query']['search_name'] = $search_name;
        $total = Db::table('equipment')->count();
		$result = Db::table('equipment')->where('name','like',"%{$search_name}%")->paginate(2,false,$search);
		$status = ['1'=>'进口材料','2'=>'国际设备'];
		$page = $result->render();
		$this->assign('total',$total);
		$this->assign('search_name',$search_name);
		$this->assign('status',$status);
		$this->assign('result',$result);
		return $this->fetch();
	}
	//设备添加和编辑
	public function add()
	{
		$id = (int)input('get.id');
		$data = Db::table('equipment')->where(array('id'=>$id))->find();
		$valID1 = 1;
        $valID2 = 2;
        $this->assign('valID1',$valID1);
        $this->assign('valID2',$valID2);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//保存
	public function save()
	{
		$id = (int)input('post.id');
        $data['name'] = trim(input('post.name'));
        $data['imga'] = trim(input('post.imga'));
        $data['imgb'] = trim(input('post.imgb'));
        $data['status'] = (int)input('post.status');
        $data['content'] = trim(input('post.content'));

        if($data['name'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'设备名称不能为空')));
        }
        if($data['content'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'设备简介不能为空')));
        }
        if($id){
            $res = Db::table('equipment')->where(array('id'=>$id))->update($data);
        }else{
            $res = Db::table('equipment')->insert($data);
        }
        if(!$res){
        	exit(json_encode(array('code'=>1,'msg'=>'保存失败')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
	// 图片上传
    public function upload_img(){
        $file = request()->file('file');
        if($file==null){
            exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
        }
        $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        $ext = ($info->getExtension());
        if(!in_array($ext,array('jpg','jpeg','gif','png'))){
            exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
        }
        $img = '/uploads/'.$info->getSaveName();
        exit(json_encode(array('code'=>0,'msg'=>$img)));
    }
    //删除
    public function delete(){
        $id = (int)input('post.id');
        $imga = Db::table('equipment')->field('imga')->where(array('id'=>$id))->find();
        $urlimga=preg_replace("/\\\/","/",".".$imga['imga']);
        unlink($urlimga);
        $imgb = Db::table('equipment')->field('imgb')->where(array('id'=>$id))->find();
        $urlimgb=preg_replace("/\\\/","/",".".$imgb['imgb']);
        unlink($urlimgb);
        $res = Db::table('equipment')->where(array('id'=>$id))->delete();
        if(!$res){
            exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
    }
}